package co.th.bjc.md.dbonline.control;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import co.th.bjc.md.dbonline.bean.HospitalDBOnlineBean;
import co.th.bjc.md.helper.HospitalHelper;
import co.th.bjc.md.model.Hospital;
import co.th.bjc.md.model.ImpRating;
import co.th.bjc.md.model.Person;
import co.th.bjc.md.model.Province;
import co.th.bjc.md.model.Region;
import co.th.bjc.md.model.RpRating;

public class HospitalDBOnlineControl {

	private static Logger _log = Logger.getLogger(HospitalDBOnlineControl.class);
	
	public HospitalDBOnlineBean saveHospital(HospitalDBOnlineBean bean){
		try{
			Hospital hospital = copyHospitalDBOnlineBeanToHospital(bean);
			HospitalHelper helper = new HospitalHelper();
			hospital = helper.save(null, hospital);
			bean = copyHospitalToHospitalDBOnlineBean(hospital);
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex.toString());
		}
		return bean;	
	}
	
	private HospitalDBOnlineBean copyHospitalToHospitalDBOnlineBean(Hospital model) throws Exception{
		HospitalDBOnlineBean bean = new HospitalDBOnlineBean();
		bean.setHospitalCode(model.getHospitalCode());
		bean.setHospitalNameTh(model.getHospitalNameTH());
		bean.setHospitalNameEn(model.getHospitalNameEN());
		bean.setHospitalType(model.getHospitalType());
		bean.setAddress(model.getAddress());
		bean.setTelephone(model.getTelephone());
		bean.setEmail(model.getEmail());
		
		if (null != model.getPresident()){
			bean.setPresidentCode(model.getPresident().getPersonCode());
			bean.setPresidentTitle(model.getPresident().getPersonTitle());
			bean.setPresidentFirstName(model.getPresident().getPersonFirstName());
			bean.setPresidentLastName(model.getPresident().getPersonLastName());
			bean.setPresidentTelephone(model.getPresident().getTelephone());
			bean.setPresidentEmail(model.getPresident().getEmail());
			bean.setPresidentIMP(model.getPresident().getImp().getImpCode());
			bean.setPresidentRP(model.getPresident().getRp().getRpCode());
			bean.setPresidentRemark(model.getPresident().getRemark());
		}
		
		bean.setProvince(model.getProvince().getProvinceId());
		bean.setRegion(model.getRegion().getRegionId());
		
		return bean;
	}
	
	private Hospital copyHospitalDBOnlineBeanToHospital(HospitalDBOnlineBean bean){
		Hospital model = new Hospital();
		model.setHospitalCode(bean.getHospitalCode());
		model.setHospitalNameTH(bean.getHospitalNameTh());
		model.setHospitalNameEN(bean.getHospitalNameEn());
		model.setHospitalType(bean.getHospitalType());
		model.setAddress(bean.getAddress());
		model.setTelephone(bean.getTelephone());
		model.setEmail(bean.getEmail());
		
		Province province = new Province(bean.getProvince(), null, null);		
		model.setProvince(province);
		
		Region region = new Region(bean.getRegion(), null, null);
		model.setRegion(region);
		
		Person president = new Person();
		president.setPersonCode(bean.getPresidentCode());
		president.setPersonTitle(bean.getPresidentTitle());
		president.setPersonFirstName(bean.getPresidentFirstName());
		president.setPersonLastName(bean.getPresidentLastName());
		president.setTelephone(bean.getPresidentTelephone());
		president.setEmail(bean.getPresidentEmail());
		president.setImp(new ImpRating(bean.getPresidentIMP(), null, null, null, null, null));
		president.setRp(new RpRating(bean.getPresidentRP(), null, null, null, null, null));
		president.setRemark(bean.getPresidentRemark());
		model.setPresident(president);
		
		return model;
	}
	
	public List<HospitalDBOnlineBean> findHospitalByRegion(BigDecimal region_id){
		List<HospitalDBOnlineBean> listHosBean = new ArrayList<HospitalDBOnlineBean>(0); 
		try{
			HospitalHelper helper = new HospitalHelper();
			List<Hospital> listHosModel = helper.findHospitalByRegion(region_id);
			
			if (listHosModel != null && !listHosModel.isEmpty()){
				for(Hospital hospital : listHosModel){
					listHosBean.add(this.copyHospitalToHospitalDBOnlineBean(hospital));
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return listHosBean;
	}
	
	public HospitalDBOnlineBean findHospitalByHospitalCode(BigDecimal hospitalCode){
		HospitalDBOnlineBean bean = null; 
		try{
			Hospital model = new HospitalHelper().findHospitalByHospitalCode(hospitalCode);
			if (model != null){
				bean = this.copyHospitalToHospitalDBOnlineBean(model);
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return bean;
	}
}
